package modules::tophat;
use modules::utilities;
use Data::Dumper;
sub new {
  my ($class,$config)    = @_;

  my $self = {
               'util' => $config,
             };
  bless $self,$class;
  return $self;
}

sub prepare {
  my ($self) = @_;
  my $util                  = $self->{'util'};
  my $job_name              = $util->{'_job_name'};
  my $path                  = $util->{'_path'};
  my $tophat_dir            = $util->{'_tophat_dir' };
  my $job_dir               = $path . '/results/' . $job_name;
  my $samples               = $util->{'_samples'   };
  print "Creating '$tophat_dir'...\n"; 
  mkdir  $tophat_dir, 777; chmod(0777, $tophat_dir);
  
  mkdir  "$path/results/", 0777 unless(-d "$path/results/"   ); chmod(0777, $job_dir   );
  
  mkdir  $job_dir   , 0777 unless(-d $job_dir   ); chmod(0777, $job_dir   );

  mkdir  $tophat_dir, 0777 unless(-d $tophat_dir); chmod(0777, $tophat_dir);
  $self->{'_tophat_dir'} = $tophat_out;

  foreach my $sample(@$samples){
    my    $sample_dir = $tophat_dir ."/". $sample->[0];
    mkdir $sample_dir, 0777 unless(-d $sample_dir);
    print "Creating directory '$sample_dir'...\n"; 
    chmod(0777, $sample_dir);
  }
  
  return;
}

sub run {
  my $self = shift;
  my $util               = $self->{'util'};
  my $samples            = $util->{'_samples'     };
  my $path               = $util->{'_path'        };
  my $genome             = $util->{'_genome'      };
  my $fastq_dir          = $util->{'_fastq_dir'   };
  my $gtf_file           = $util->{'_gtf_file'    };
  my $tophat_out         = $util->{'_tophat_dir'  };


  my $i=1;
  foreach my $sample(@$samples) { 

    my    $cmd  =  qq(bsub -J tophat_$i -n 4 -R "span[ptile=4] rusage[mem=5000]" ) 
                .  qq(/usr/users/ga002/corpasm/workarea/bin/tophat -p 8 -G $gtf_file -r 100 )
                .  qq(--mate-std-dev 80 --min-anchor-length 12 --max-multihits 20  -o $tophat_out/$sample->[0] );
    #map { $cmd  .= qq($_ $tophat_conf->{$_} )   }  keys %$tophat_conf; # map each option
          $cmd  .= qq($genome );
    map { $cmd  .= qq($fastq_dir/$_  )    }  @$sample; # map each replica to the command
    print "\n\nRUNNING COMMAND:\n$cmd\n\n";
    system($cmd);
    $i++;
  }

  my $utility = modules::utilities->new();
  $utility->check_completion('tophat');

  return;
}

1; 
